package com.lframework.xingyun.basedata.impl.product;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.ProductAssistance;
import com.lframework.xingyun.basedata.mappers.ProductAssistanceMapper;
import com.lframework.xingyun.basedata.service.product.ProductAssistanceService;
import com.lframework.xingyun.basedata.vo.product.assistance.CreateProductAssistanceVo;
import com.lframework.xingyun.basedata.vo.product.assistance.QueryProductAssistanceVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductAssistanceServiceImpl extends BaseMpServiceImpl<ProductAssistanceMapper, ProductAssistance> implements ProductAssistanceService {

    @Override
    public List<ProductAssistance> query(QueryProductAssistanceVo vo) {
        return getBaseMapper().query(vo);
    }

    @Cacheable(value = ProductAssistance.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public ProductAssistance findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增商品辅料表，ID：{}", params = {"#ids"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateProductAssistanceVo vo) {

        getThis(ProductAssistanceService.class).remove(Wrappers.lambdaUpdate(ProductAssistance.class)
                .eq(ProductAssistance::getProductMainId, vo.getProductMainId()));

        List<ProductAssistance> list = new ArrayList<>();
        for (int i = 0; i < vo.getProductIds().size(); i++) {
            ProductAssistance data = new ProductAssistance();
            data.setProductMainId(vo.getProductMainId());
            data.setProductId(vo.getProductIds().get(i));
            data.setDisplayNum(i);
            list.add(data);
        }

        getThis(ProductAssistanceService.class).saveBatch(list);

        OpLogUtil.setVariable("ids", list.stream().map(ProductAssistance::getId).collect(Collectors.joining(",")));
        OpLogUtil.setExtra(vo);

        return vo.getProductMainId();
    }

    @CacheEvict(value = ProductAssistance.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

